package com.bgrconsulting.stockorderentry.service.client.tests;

import java.net.URI;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.bgrconsulting.stockorderentry.service.converter.AccountConverter;
import com.bgrconsulting.stockorderentry.service.converter.PlaceEquityOrderConverter;
import com.bgrconsulting.stockorderentry.service.converter.response.AccountBalanceResponseConverter;
import com.bgrconsulting.stockorderentry.service.model.Account;
import com.bgrconsulting.stockorderentry.service.model.AccountBalance;
import com.bgrconsulting.stockorderentry.service.model.CashAccountBalance;
import com.bgrconsulting.stockorderentry.service.model.DTBalance;
import com.bgrconsulting.stockorderentry.service.model.MarginAccountBalance;
import com.bgrconsulting.stockorderentry.service.model.action.PlaceEquityOrder;
import com.bgrconsulting.stockorderentry.service.model.request.EquityOrderRequest;
import com.bgrconsulting.stockorderentry.service.model.response.AccountBalanceResponse;
import com.google.appengine.api.datastore.KeyFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.representation.Form;

public class ClientTests {
	
	
	public void ClientAddPlaceEquityOrderRequestTest() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());
		
		// Create 
		EquityOrderRequest eqr = new EquityOrderRequest();
		eqr.setAccountId("8349577");
		eqr.setAllOrNone(true);
		eqr.setClientOrderId("12345678");
		eqr.setLimitPrice(3.00);
		
		PlaceEquityOrder peo = new PlaceEquityOrder(eqr);
		
		PlaceEquityOrderConverter peoc = new PlaceEquityOrderConverter(peo);
		
		ClientResponse response = service.path("rest").path("placeequityorder").accept(MediaType.APPLICATION_XML).put(ClientResponse.class, peoc);
		
		// Return code should be 201 == created resource
		System.out.println(response.getStatus());
		
		// Get the Todos
		System.out.println(service.path("rest").path("todos").accept(
				MediaType.TEXT_XML).get(String.class));
		// Get XML for application
		System.out.println(service.path("rest").path("todos").accept(
				MediaType.APPLICATION_JSON).get(String.class));
		// Get JSON for application
		System.out.println(service.path("rest").path("todos").accept(
				MediaType.APPLICATION_XML).get(String.class));
		
		// Get the  Todo with id 1
		System.out.println(service.path("rest").path("todos/1").accept(
				MediaType.APPLICATION_XML).get(String.class));
		// get Todo with id 1 
		service.path("rest").path("todos/3").delete();
		// Get the all todos, id 1 should be deleted
		System.out.println(service.path("rest").path("todos").accept(
				MediaType.APPLICATION_XML).get(String.class));
		
		
		
		
	}
	
	@Test
	public void AddAccountTest() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());
		
		String id = "999999991J";
		
		// Create 
		Account account = null;
		
		AccountBalance ab = new AccountBalance();
		ab.setCashAvailableForWithDrawal(0.00);
		ab.setNetAccountValue(-663.20);
		ab.setNetCash(740.450013);
		ab.setSweepDepositAmount(0.00);
		ab.setTotalLongValue(122.23);
		ab.setTotalSecurituesMktValue(-454.22);
		ab.setAccountId(id);
		
		
		DTBalance dtb = new DTBalance();
		dtb.setDTCashBalance(0.00);
		dtb.setDTMarginBalance(0.00);
		dtb.setDTMarginSecurities(-4183.79);
		dtb.setDTNonMarginableSecuritiesAndOptions(-2714.88);
		dtb.setDTStatus("QIALIFIED_4_X");
		dtb.setAccountId(id);
		//dtb.setKey(KeyFactory.createKey("DTBalance", id));
		
		MarginAccountBalance mab = new MarginAccountBalance();
		
		mab.setDTBalance(dtb);
		mab.setFedCall(-105.49);
		mab.setMarginBalance(-740.45);
		mab.setMarginBalanceWithDrawal(-2714.88);
		mab.setMarginEquity(-714.88);
		mab.setMarginableSecurities(-4183.73);
		mab.setMaxAvailableForWithDrawal(-2714.88);
		mab.setMinEquityCall(-25663.20);
		mab.setNonMarginableSecuritiesAndOptions(-2714.88);
		mab.setAccountId(id);
		//mab.setKey(KeyFactory.createKey("MarginAccountBalance", id));
		
		
		CashAccountBalance cab = new CashAccountBalance();
		
		cab.setCashAvailableForInvestment(1000.23);
		cab.setCashBalance(203432.23);
		cab.setSettledCashForInvestment(12323.12);
		cab.setAccountId(id);
		//cab.setKey(KeyFactory.createKey("CashAccountBalance", id));
		
		account = new Account("briviere@gmail.com", "INDIVUAL", 
				id, 
				"MARGIN", 
				"", 
				657.37, 
				"INDIVIDUAL", 
				ab, 
				dtb, 
				mab, 
				cab);
		
		//account.setKey(KeyFactory.createKey("Account", id));
		
		ClientResponse response = service.path("rest").path("account").accept(MediaType.APPLICATION_XML).put(ClientResponse.class, account);
		
		//ClientResponse response = service.path("rest").path("account").path(ac.getAccountId()).accept(MediaType.APPLICATION_XML).put(ClientResponse.class, ac);
		
		//ClientResponse response = service.path(ac.getAccountId()).accept(MediaType.APPLICATION_XML).put(ClientResponse.class, ac);
		
		
		// Return code should be 201 == created resource
		System.out.println(response.getStatus());
		
		//Assert.assertEquals("201", response.getStatus());
		
		System.out.println(service.path("rest").path("account").path(account.getAccountId()).accept(MediaType.APPLICATION_JSON).get(String.class));
		

		ab.setAccountId("7777777J");
	    dtb.setAccountId("7777777J");
	    mab.setAccountId("7777777J");
	    cab.setAccountId("7777777J");
		
		
		
		account = new Account("briviere@gmail.com", "INDIVUAL", 
				"7777777J", 
				"MARGIN", 
				"", 
				657.37, 
				"INDIVIDUAL", 
				ab, 
				dtb, 
				mab, 
				cab);
		
		response = service.path("rest").path("account").accept(MediaType.APPLICATION_XML).put(ClientResponse.class, account);
		
		// Return code should be 201 == created resource
		System.out.println(response.getStatus());
		
		//Assert.assertEquals("201", response.getStatus());
		
		System.out.println(service.path("rest").path("account").path(account.getAccountId()).accept(MediaType.APPLICATION_JSON).get(String.class));
		
		ab.setAccountId("6666666J");
	    dtb.setAccountId("6666666J");
	    mab.setAccountId("6666666J");
	    cab.setAccountId("6666666J");
		
		account = new Account("briviere@gmail.com", "INDIVUAL", 
				"6666666J", 
				"MARGIN", 
				"", 
				657.37, 
				"INDIVIDUAL", 
				ab, 
				dtb, 
				mab, 
				cab);
		
		response = service.path("rest").path("account").accept(MediaType.APPLICATION_XML).put(ClientResponse.class, account);
		
		// Return code should be 201 == created resource
		System.out.println(response.getStatus());
		
		//Assert.assertEquals("201", response.getStatus());
		
		System.out.println(service.path("rest").path("account").path(account.getAccountId()).accept(MediaType.APPLICATION_JSON).get(String.class));
		
		
		System.out.println(service.path("rest").path("accountlist").accept(MediaType.APPLICATION_JSON).get(String.class));
		
	}
	
	
	public void AddAccountListTest() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());
		
		String id = "999999991J";
		
		// Create 
		Account account = null;
		
		AccountBalance ab = new AccountBalance();
		ab.setCashAvailableForWithDrawal(0.00);
		ab.setNetAccountValue(-663.20);
		ab.setNetCash(740.450013);
		ab.setSweepDepositAmount(0.00);
		ab.setTotalLongValue(122.23);
		ab.setTotalSecurituesMktValue(-454.22);
		ab.setAccountId(id);
		
		
		DTBalance dtb = new DTBalance();
		dtb.setDTCashBalance(0.00);
		dtb.setDTMarginBalance(0.00);
		dtb.setDTMarginSecurities(-4183.79);
		dtb.setDTNonMarginableSecuritiesAndOptions(-2714.88);
		dtb.setDTStatus("QIALIFIED_4_X");
		dtb.setAccountId(id);
		//dtb.setKey(KeyFactory.createKey("DTBalance", id));
		
		MarginAccountBalance mab = new MarginAccountBalance();
		
		mab.setDTBalance(dtb);
		mab.setFedCall(-105.49);
		mab.setMarginBalance(-740.45);
		mab.setMarginBalanceWithDrawal(-2714.88);
		mab.setMarginEquity(-714.88);
		mab.setMarginableSecurities(-4183.73);
		mab.setMaxAvailableForWithDrawal(-2714.88);
		mab.setMinEquityCall(-25663.20);
		mab.setNonMarginableSecuritiesAndOptions(-2714.88);
		mab.setAccountId(id);
		//mab.setKey(KeyFactory.createKey("MarginAccountBalance", id));
		
		
		CashAccountBalance cab = new CashAccountBalance();
		
		cab.setCashAvailableForInvestment(1000.23);
		cab.setCashBalance(203432.23);
		cab.setSettledCashForInvestment(12323.12);
		cab.setAccountId(id);
		//cab.setKey(KeyFactory.createKey("CashAccountBalance", id));
		
		account = new Account("briviere@gmail.com", "INDIVUAL", 
				id, 
				"MARGIN", 
				"", 
				657.37, 
				"INDIVIDUAL", 
				ab, 
				dtb, 
				mab, 
				cab);
		
		//account.setKey(KeyFactory.createKey("Account", id));
		
		ClientResponse response = service.path("rest").path("account").accept(MediaType.APPLICATION_XML).put(ClientResponse.class, account);
		
		//ClientResponse response = service.path("rest").path("account").path(ac.getAccountId()).accept(MediaType.APPLICATION_XML).put(ClientResponse.class, ac);
		
		//ClientResponse response = service.path(ac.getAccountId()).accept(MediaType.APPLICATION_XML).put(ClientResponse.class, ac);
		
		
		// Return code should be 201 == created resource
		System.out.println(response.getStatus());
		
		//Assert.assertEquals("201", response.getStatus());
		
		System.out.println(service.path("rest").path("account").path(account.getAccountId()).accept(MediaType.APPLICATION_JSON).get(String.class));
		
	}
	
	
	public void AddAccountBalanceTest() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());
		
		Account ac = new Account("INDIVUAL", "123456789J", "MARGIN", "", 657.37, "INDIVIDUAL");
		AccountBalance ab = new AccountBalance();
		ab.setCashAvailableForWithDrawal(0.00);
		ab.setNetAccountValue(-663.20);
		ab.setNetCash(740.450013);
		ab.setSweepDepositAmount(0.00);
		ab.setTotalLongValue(122.23);
		ab.setTotalSecurituesMktValue(-454.22);
		
		DTBalance dtb = new DTBalance();
		dtb.setDTCashBalance(0.00);
		dtb.setDTMarginBalance(0.00);
		dtb.setDTMarginSecurities(-4183.79);
		dtb.setDTNonMarginableSecuritiesAndOptions(-2714.88);
		dtb.setDTStatus("QIALIFIED_4_X");
		
		MarginAccountBalance mab = new MarginAccountBalance();
		
		mab.setDTBalance(dtb);
		mab.setFedCall(-105.49);
		mab.setMarginBalance(-740.45);
		mab.setMarginBalanceWithDrawal(-2714.88);
		mab.setMarginEquity(-714.88);
		mab.setMarginableSecurities(-4183.73);
		mab.setMaxAvailableForWithDrawal(-2714.88);
		mab.setMinEquityCall(-25663.20);
		mab.setNonMarginableSecuritiesAndOptions(-2714.88);
		
		
		CashAccountBalance cab = new CashAccountBalance();
		
		cab.setCashAvailableForInvestment(1000.23);
		cab.setCashBalance(203432.23);
		cab.setSettledCashForInvestment(12323.12);
		
		
		AccountBalanceResponse abr = new AccountBalanceResponse(ac, ab, mab, cab);
		
		//AccountBalanceResponseConverter abrc = new AccountBalanceResponseConverter(abr);
		
		ClientResponse response = service.path("rest").path("accountbalance").accept(MediaType.APPLICATION_XML).put(ClientResponse.class, abr);
		
		//ClientResponse response = service.path("rest").path("account").path(ac.getAccountId()).accept(MediaType.APPLICATION_XML).put(ClientResponse.class, ac);
		
		//ClientResponse response = service.path(ac.getAccountId()).accept(MediaType.APPLICATION_XML).put(ClientResponse.class, ac);
		
		
		// Return code should be 201 == created resource
		System.out.println(response.getStatus());
		
		//Assert.assertEquals("201", response.getStatus());
		
		
		System.out.println(service.path("rest").path("accountbalance").path(abr.getAccount().getAccountId()).accept(MediaType.APPLICATION_JSON).get(String.class));
		
	}
	
	
	
	
	private static URI getBaseURI() {
		return UriBuilder.fromUri(
				"http://localhost:8888/resources/accounts/").build();
	}
		
}

